package org.example.server.service;

import cn.hutool.core.util.IdUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.server.bean.PictureBean;
import org.example.server.mapper.PictureMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PictureService {

    @Resource
    private PictureMapper pictureMapper;

    public void save(PictureBean pictureBean){
        if (pictureBean.getTitle()==null){
            throw new RuntimeException("标题不能为空");
        }
        if (pictureBean.getUrl()==null){
            throw new RuntimeException("图片地址不能为空");
        }
        if (pictureBean.getOriginTitle()==null){
            throw new RuntimeException("原始标题不能为空");
        }

        pictureBean.setId(IdUtil.getSnowflakeNextId());
        pictureBean.setCreateTime(System.currentTimeMillis());
        pictureBean.setUpdateTime(System.currentTimeMillis());

        int inserted = pictureMapper.insert(pictureBean);
        if (inserted<=0){
            throw new RuntimeException("保存图片失败");
        }

    }

    public List<PictureBean> list(){
        return pictureMapper.select(new PictureBean());
    }

}
